//
//  WBWelcomeViewController.swift
//  weibo
//
//  Created by yanghong on 15/12/9.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WBWelcomeViewController: UIViewController {
    
    override func loadView() {
        
        view = backGroundIMG
        
        setupUI()
    }
    
    //更新UI
    private func setupUI() {
        // 添加控件
        view.addSubview(headIcon)
        view.addSubview(welcomeBackLabel)
        //添加约束
        // headIcon
        headIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        // welcomeBackLabel
        welcomeBackLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headIcon)
            make.top.equalTo(headIcon.snp_bottom).offset(16)
            
        }
        
    }
    
    // MARK -  在viewDidAppere 里面添加动画
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        //更新约束
        
        headIcon.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
            
        }
        welcomeBackLabel.alpha = 0
        
        //弹簧动画效果
        
        UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.welcomeBackLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        //在这里可以发送通知跳转到首页
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
                        
                })
                
        }
        
    }
    // MARK -  懒加载控件
    // 背景图片
    private var backGroundIMG: UIImageView = {
        
        let bgIMG = UIImage(named: "ad_background")
        return UIImageView(image: bgIMG)
    }()
    
    //默认头像
    private var headIcon: UIImageView = {
        
        //根据用户信息下载头像
        
        
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imgView.sd_setImageWithURL(NSURL(string: (WBUserAccountViewModel.sharedAccount.userAccount?.avatar_large)!)!, placeholderImage: UIImage(named: "avatar_default_big"))
        
        imgView.layer.cornerRadius = 45
        
        imgView.layer.masksToBounds = true
        
        return imgView
    }()
    
    //欢迎回来label
    private var welcomeBackLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "欢迎回来"
        
        label.textColor = UIColor.darkGrayColor()
        
        label.font =  UIFont.systemFontOfSize(17)
        
        return label
    }()
}
